package com.tust.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tust.app.domain.News;
import com.tust.app.service.NewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
public class NewsController {

    @Autowired
    private NewService newsService;


    @RequestMapping("/page")
    public String page(HttpServletRequest request) {

        PageHelper.startPage(1, 3);
        //查询数据库,得到新闻表中的数据列表,丢给页面
        List<News> news = newsService.getAllNews();
        PageInfo page = new PageInfo(news);
        request.setAttribute("pageInfo", page);

        return "home";

    }

    @RequestMapping("/page/{pageNum}")
    public String page2(@PathVariable("pageNum") Integer pageNum, HttpServletRequest request) {

        PageHelper.startPage(pageNum, 3);
        //查询数据库,得到新闻表中的数据列表,丢给页面
        List<News> news = newsService.getAllNews();
        PageInfo page = new PageInfo(news);
        request.setAttribute("pageInfo", page);
        return "home";

    }


    @RequestMapping("/anews")
    public String anews(Integer pn, HttpServletRequest request) {
        if (pn == null) {
            pn = 1;
        }
        PageHelper.startPage(pn, 3);
        List<News> news = newsService.getAllNews();
        PageInfo page = new PageInfo((news));
        request.setAttribute("news", page);
        return "admin/news/adminPageList";

    }


    @RequestMapping("/anews/{pn}")
    public String anews2(@PathVariable("pn") Integer pageNum, HttpServletRequest request) {
        PageHelper.startPage(pageNum, 3);
        List<News> news = newsService.getAllNews();
        PageInfo page = new PageInfo((news));
        request.setAttribute("news", page);
        return "admin/news/adminPageList";


    }

    @RequestMapping("/selectByTitle")
    public String selectByTitle(String title, HttpServletRequest request) {
        //根据title去查询相关的文章

        PageHelper.startPage(1, 8);
        List<News> searchList = newsService.search(title);
        PageInfo pageInfo = new PageInfo(searchList);
        request.setAttribute("news", pageInfo);

        System.out.println(title);
        if (searchList != null) {
            return "admin/news/adminPageList";
        }
        return "error";

    }


    @RequestMapping("/writeNews")
    public String writeNews(String title, String article, HttpServletRequest request) {
        //构造一个news对象装数据
        News news = new News();
        news.setTitle(title);
        news.setArticle(article);
        Date date = new Date();

        SimpleDateFormat format = new SimpleDateFormat();
        String format1 = format.format(date);
        news.setTime(format1);

        Integer i = newsService.addArticle(news);
//        System.out.println("i: "+i);
        if (i > 0) {
            //查询所有的文章，传递给页面
            PageHelper.startPage(1, 10);

            List<News> news1 = newsService.getAllNews();
            PageInfo pageInfo = new PageInfo(news1);
            request.setAttribute("news", pageInfo);
            return "admin/news/adminPageList";
        }
        return "error";

//        System.out.println(title);
//        System.out.println(article);

    }


    @RequestMapping("/getNews")
    public String getNews(Integer id, HttpServletRequest request) {
        System.out.println("id: "+id);
        //根据id获取对应的新闻信息
        News news = newsService.findNewsById(id);

        //把查询出来的信息
        request.setAttribute("news", news);

        return "admin/news/editNews";

    }


    @RequestMapping("/updateNews")
    public String updateNews(Integer id, String title, String article) {
        //实现修改过程
        News news = new News();
        news.setId(id);
        news.setTitle(title);
        news.setArticle(article);
        boolean b = newsService.modifyNews(news);
        if (b) {
            return "forward:/anews";
        }

        return "error";

    }

    @RequestMapping("/deleteNewsById")
    public String deleteNewsById(Integer id){

        boolean b=newsService.delNews(id);
        if(b){

            return "forward:/anews";
        }

        return "error";


    }

    @RequestMapping("/selectNewsById")
    public String selectNewsById(Integer id,HttpServletRequest request){

        //根据id查询一条新闻消息
        News news = newsService.findNewsById(id);
        //把news对象传递给页面显示
        request.setAttribute("news",news);

        return "detail";


    }



}
